package com.collect.tanzania.zhiScene.zhiCollection.zhiGroup.zhiDaily;

import android.annotation.SuppressLint;
import android.app.Activity;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.collect.tanzania.R;
import com.collect.tanzania.zhiAdater.zhiDaily.ZHIDailyListAdjuster;
import com.collect.tanzania.zhiBase.BaseFragment;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRCaseEntity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRCaseListEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISDailyCaseEntity;
import com.collect.tanzania.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.tanzania.zhiHelper.ZHIMainProcessHelper;
import com.collect.tanzania.zhiScene.zhiCollection.zhiGroup.zhiRoot.ZHIRootGroupFragmentInterface;
import com.collect.tanzania.zhiUtils.MenuUtil;

import java.util.ArrayList;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIDailyGroupFragment extends BaseFragment<ZHIDailyGroupPresenter> implements ZHIDailyGroupContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.rc_list)
    RecyclerView rc_list;

    @BindView(R.id.tv_page_title)
    TextView tv_page_title;

    @BindView(R.id.iv_filter)
    ImageView iv_filter;

    @BindView(R.id.iv_words)
    ImageView iv_words;

    @BindView(R.id.iv_products)
    ImageView iv_products;

    private PopupMenu mPopupMenu;

    private static int mTotalCount = 0;

    private final ZHIDailyListAdjuster mZHIDailyListAdjuster = new ZHIDailyListAdjuster();

    private ArrayList<ZHIRCaseEntity> mDailyEntityArrayList;

    private ZHIRootGroupFragmentInterface mZHIRootGroupFragmentInterface;

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_daily;
    }

    @Override
    protected void initView(View view) {
        srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));
        iv_words.setOnClickListener(v -> ZHIProjectRouter.toWordsSkills());

        initData(true);
        initEvent();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mZHIRootGroupFragmentInterface = (ZHIRootGroupFragmentInterface) activity;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (ZHIMainProcessHelper.getInstance().isHadNewCaseRecord()) {
            ZHIMainProcessHelper.getInstance().setHadNewCaseRecord(false);
            initData(true);
        }
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onGetLatestCaseListV2(ZHIRCaseListEntity caseListEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            mDailyEntityArrayList = caseListEntity.getDunList();
            mZHIDailyListAdjuster.setNewData(mDailyEntityArrayList);

            mTotalCount = mDailyEntityArrayList.size();
            Log.d(TAG, "onGetLatestCaseListV2: " + caseListEntity.getDunList().size());

            String narText = "Total: " + mTotalCount;
            tv_page_title.setText(narText);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("InflateParams")
    public void initEvent() {
        try {
            srl_swipe_layout.setOnRefreshListener(() -> initData(false));

            mZHIDailyListAdjuster.setEmptyView(getRefreshEmptyView());
            mZHIDailyListAdjuster.setOnOrderItemClickListener(caseEntity -> ZHIProjectRouter.toDetailV2(caseEntity.getUserId(), caseEntity.getOrderId(), caseEntity.getAppId()));

            rc_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_list.setAdapter(mZHIDailyListAdjuster);

            iv_filter.setOnClickListener(v -> {
                if (mZHIRootGroupFragmentInterface != null) {
                    Log.d(TAG, "iv_filter: ==========");
                    mZHIRootGroupFragmentInterface.onShowFilterDaily();
                }
            });

            iv_products.setOnClickListener(this::showPopMenuView);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int dataCount() {
        return mTotalCount;
    }

    public void initData(boolean showDialog) {
        try {
            ZHISDailyCaseEntity zhisDailyCaseEntity = getCaseSearchParams();
            if (zhisDailyCaseEntity == null) {
                srl_swipe_layout.setRefreshing(false);
                return;
            }

            assert mPresenter != null;
            mPresenter.getLatestCaseListV2(getActivity(), zhisDailyCaseEntity, showDialog);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void refreshData(ZHISDailyCaseEntity zhisDailyCaseEntity) {
        if (mPresenter != null && zhisDailyCaseEntity != null) {
            mPresenter.getLatestCaseListV2(getActivity(), zhisDailyCaseEntity, true);
        }
    }

    public void refreshData() {
        initData(true);
    }

    private ZHISDailyCaseEntity getCaseSearchParams() {
        if (mZHIRootGroupFragmentInterface != null) {
            return mZHIRootGroupFragmentInterface.onGetDailyFilterParams();
        }
        return null;
    }

    public void updateProductListInfo(String product) {
        try {
            if (mDailyEntityArrayList != null && mDailyEntityArrayList.size() > 0) {
                ArrayList<ZHIRCaseEntity> productArray = new ArrayList<>();
                for (int i = 0; i < mDailyEntityArrayList.size(); i++) {
                    ZHIRCaseEntity zhirCaseEntity = mDailyEntityArrayList.get(i);
                    if (zhirCaseEntity.getAppId().equalsIgnoreCase(product.toUpperCase())) {
                        productArray.add(zhirCaseEntity);
                    }
                }
                if (mZHIDailyListAdjuster != null && productArray.size() > 0) {
                    mZHIDailyListAdjuster.setNewData(productArray);
                } else {
                    Log.d(TAG, "updateProductListInfo: NO Found Product");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> {
            initData(true);
        });
        return emptyView;
    }

    private void showPopMenuView(View view) {
        if (mPopupMenu == null) {
            mPopupMenu = new PopupMenu(getActivity(), view);
            mPopupMenu = MenuUtil.getProductMenu(getActivity(), view, item -> {
                String menuItem = item.getTitle().toString();
                Log.d(TAG, "showPopMenuView: " + menuItem);
                updateProductListInfo(menuItem);
                return true;
            });
        }
        mPopupMenu.show();
    }
}
